package week5.day30;

/**
 * @Author fengzihao
 * @Date 2024-05-14 20:36
 */
public class ReverseLinkedNode {
    public static void main(String[] args) {
        // 翻转链表
        // a --> b --> c --> d --> e
        Node e = new Node("e");
        Node d = new Node("d", e);
        Node c = new Node("c", d);
        Node b = new Node("b", c);
        Node a = new Node("a", b);

        // e  --> d  -->  c  --> b  --> a
        Node newLinked = reLinked(a);

        System.out.println(newLinked);
    }
    /**
     * 翻转链表
     * @param head : 原链表头结点
     * @return : 经过翻转链表的头结点
     */
    private static Node reLinked(Node head) {
        if (head ==null)
            return null;
        if (head.next==null)
            return head;
        Node tmpNode = new Node();
        tmpNode.next = head;
        head = head.next;
        tmpNode.next.next=null;
        while (head!=null){
            Node node = head;
            head = head.next;
            node.next = tmpNode.next;
            tmpNode.next = node;
        }
        return tmpNode.next;
    }
}
